public class newbuttonoverride{

 public final String FIELD_CITY= 'city__c';
 
 public newbuttonoverride(ApexPages.StandardController controller) {
        this.controller = controller;
    }    
    public pagereference newnew(){
        List<place__c> city;
        id cityid;
        string name;
        Field_Info__c fieldid=[select Field_ID__c from Field_Info__c where name=: Field_city and SobjectType__c='Hospital__c'];
        id uid=userinfo.getuserid();
        List<user> cityname=[select id,usercity__c from user where id=:uid];
        if(cityname[0].usercity__c!=null){
            name=cityname[0].usercity__c.trim();
            city=[select id from place__c where PlaceType__c=:'City' and name=:name];
            if(city.size()>0){
                cityid=city[0].id;
            }
          
           
            
        }
        
     //   list<hospital__c> hospitall=[select id,city__c from hospital__c];
        PageReference ref=null;
        ref=new PageReference('/a0c/e?retURL=%2Fa0c&nooverride=1');  
        ref.getParameters().put('CF'+fieldid.field_id__c,name); //00N90000000u4YW
        ref.getParameters().put('CF'+fieldid.field_id__c+'_lkid',cityid);        //CF00NO0000000JhOw_lkid
        ref.setRedirect(true);
        return ref;
        
  }  
    private final ApexPages.StandardController controller;

 
 static testMethod void newbuttonoverride()
 {
     hospital__c add = new hospital__c();
     ApexPages.StandardController del = new ApexPages.StandardController(add);
     newbuttonoverride testCase = new newbuttonoverride(del); 
     testCase.newnew();
 }



/*     以下为旧写法，可生效，但硬编码
    public newbuttonoverride(ApexPages.StandardController controller) {
        this.controller = controller;
    }    
    public pagereference newnew(){
        List<place__c> city;
        id cityid;
        string name;
        id uid=userinfo.getuserid();
        List<user> cityname=[select id,usercity__c from user where id=:uid];
        if(cityname[0].usercity__c!=null){
            name=cityname[0].usercity__c.trim();
            city=[select id from place__c where PlaceType__c=:'City' and name=:name];
            if(city.size()>0){
                cityid=city[0].id;
            }
        }
        
     //   list<hospital__c> hospitall=[select id,city__c from hospital__c];
        PageReference ref=null;
        ref=new PageReference('/a0c/e?retURL=%2Fa0c&nooverride=1');  
        ref.getParameters().put('CF00N90000000u4YW',name); //00N90000000u4YW
        ref.getParameters().put('CF00N90000000u4YW_lkid',cityid);        //CF00NO0000000JhOw_lkid
        ref.setRedirect(true);
        return ref;
        
  }  
    private final ApexPages.StandardController controller;

 
 static testMethod void newbuttonoverride()
 {
     hospital__c add = new hospital__c();
     ApexPages.StandardController del = new ApexPages.StandardController(add);
     newbuttonoverride testCase = new newbuttonoverride(del); 
     testCase.newnew();
 }*/
 
 
 }